package com.app.controller.backstage;

import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.service.AdminUserService;
import com.app.service.StatisticsService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RestController
@RequestMapping("/admin/statistics")
public class StatisticsController {
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private AdminUserService adminUserService;

    //村级活动数量/本月新增、在线学习文章数量/本月新增、用户数/本月新增、积分商城商品数/本月新增
    @GetMapping("/selectCount")
    public Result<Object> selectCount(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.selectCount(admin.getVillageId()));
    }

    //村级活动分类情况
    @GetMapping("/getActivityTypeCount")
    public Result<Object> getActivityTypeCount(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.getActivityTypeCount(admin.getVillageId()));
    }

    //在线学习文章分类情况
    @GetMapping("/getStudyTypeCount")
    public Result<Object> getStudyTypeCount(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.getStudyTypeCount(admin.getVillageId()));
    }

    //积分商城商品分类情况
    @GetMapping("/getGoodsTypeCount")
    public Result<Object> getGoodsTypeCount(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.getGoodsTypeCount(admin.getVillageId()));
    }

    //近15天村级活动报名数量趋势统计
    @GetMapping("/activityFifteenDays")
    public Result<Object> activityFifteenDays(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.activityFifteenDays(admin.getVillageId()));
    }

    //近15天积分兑换商品数量趋势统计
    @GetMapping("/goodsFifteenDays")
    public Result<Object> goodsFifteenDays(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.goodsFifteenDays(admin.getVillageId()));
    }

    //用户当前积分前十
    @GetMapping("/getIntegralList")
    public Result<Object> getIntegralList(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.getIntegralList(admin.getVillageId()));
    }

    //用户兑换商品消费积分前十
    @GetMapping("/getConsumption")
    public Result<Object> getConsumption(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.getConsumption(admin.getVillageId()));
    }

    //用户概览      注册用户数量、报名用户数量、获取积分用户、实名用户数量、消费积分用户、文章浏览数量
    @GetMapping("/getUserOverview")
    public Result<Object> getUserOverview(HttpServletRequest request) {
        AdminUserModel admin = getAdmin(request);
        return Result.success(statisticsService.getUserOverview(admin.getVillageId()));
    }

    private AdminUserModel getAdmin(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return new AdminUserModel();
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return new AdminUserModel();
        }
        return user;
    }
}
